﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace VirtualKeyboards
{
    enum RockerDirection
    {
        top,topRight,topLeft,left,right,bottom,bottomRight,bottomLeft
    }
    public partial class VirtualKeyboard : Form
    {
        static bool isRocker = false;
        public VirtualKeyboard()
        {
            InitializeComponent();

            this.MouseDown += (sender, e) =>
            {
                EventHelper.ReleaseCapture();
                EventHelper.SendMessage(this.Handle, EventHelper.WM_SYSCOMMAND, EventHelper.SC_MOVE + EventHelper.HTCAPTION, 0);
            };
            btn_i.MouseDown += (sender, e) => {
                EventHelper.ReleaseCapture();
                EventHelper.SendMessage(this.Handle, EventHelper.WM_SYSCOMMAND, EventHelper.SC_MOVE + EventHelper.HTCAPTION, 0);
            };
            btn_r.MouseDown += (sender, e) => {
                btn_r.Cursor = Cursors.NoMove2D;
            };
            btn_r.MouseUp += (sender, e) => {
                EventHelper.key_down(Keys.R);
                btn_r.Cursor = Cursors.Hand;
                EventHelper.key_up(Keys.R);
            };


            btn_q.MouseDown += (sender, e) => { EventHelper.key_down(Keys.Q); };
            btn_w.MouseDown += (sender, e) => { EventHelper.key_down(Keys.W); };
            btn_e.MouseDown += (sender, e) => { EventHelper.key_down(Keys.E); };
            btn_a.MouseDown += (sender, e) => { EventHelper.key_down(Keys.A); };
            btn_space.MouseDown += (sender, e) => { EventHelper.key_down(Keys.Space); };
            btn_d.MouseDown += (sender, e) => { EventHelper.key_down(Keys.D); };
            btn_z.MouseDown += (sender, e) => { EventHelper.key_down(Keys.Z); };
            btn_s.MouseDown += (sender, e) => { EventHelper.key_down(Keys.S); };
            btn_b.MouseDown += (sender, e) => { EventHelper.key_down(Keys.B); };
            btn_f.MouseDown += (sender, e) => { EventHelper.key_down(Keys.F); };
            //btn_i.MouseDown += (sender, e) => { EventHelper.key_down(Keys.I); };
            btn_esc.MouseDown += (sender, e) => { EventHelper.key_down(Keys.Escape); };

            btn_q.MouseUp += (sender, e) => { EventHelper.key_up(Keys.Q); };
            btn_w.MouseUp += (sender, e) => { EventHelper.key_up(Keys.W); };
            btn_e.MouseUp += (sender, e) => { EventHelper.key_up(Keys.E); };
            btn_a.MouseUp += (sender, e) => { EventHelper.key_up(Keys.A); };
            btn_space.MouseUp += (sender, e) => { EventHelper.key_up(Keys.Space); };
            btn_d.MouseUp += (sender, e) => { EventHelper.key_up(Keys.D); };
            btn_z.MouseUp += (sender, e) => { EventHelper.key_up(Keys.Z); };
            btn_s.MouseUp += (sender, e) => { EventHelper.key_up(Keys.S); };
            btn_b.MouseUp += (sender, e) => { EventHelper.key_up(Keys.B); };
            btn_f.MouseUp += (sender, e) => { EventHelper.key_up(Keys.F); };
            //btn_i.MouseUp += (sender, e) => { EventHelper.key_up(Keys.I); };
            btn_esc.MouseUp += (sender, e) => { EventHelper.key_up(Keys.Escape); };

            // 摇杆模拟
            btn_rocker.MouseDown += Btn_rocker_MouseDown;
            btn_rocker.MouseUp += Btn_rocker_MouseUp;
            btn_rocker.MouseMove += Btn_rocker_MouseMove;

            // 鼠标离开事件
            MouseLeave+=(s,e) => {
                rockerRun = false;
                EventHelper.key_up(Keys.W);
                EventHelper.key_up(Keys.A);
                EventHelper.key_up(Keys.S);
                EventHelper.key_up(Keys.D);
            };

            SetVisible(true);

            notifyIcon1.Click += (sender, e) => { SetVisible(!this.Visible); };
        }

        static Point rockerStart = new Point(0,0);
        static bool rockerRun = false;
        private void Btn_rocker_MouseMove(object sender, MouseEventArgs e)
        {
            if (rockerRun)
            {
                var cX = e.X-rockerStart.X;
                var cY = e.Y-rockerStart.Y;
                if (cX*cX+cY*cY < 9) return;
                // tan25 = 0.466
                // tan65 = 2.144
                if (cX > 0 && cY <= 0)
                {
                    // 右上
                    if (-cY / cX <= 0.466)
                    {
                        RockerDown(RockerDirection.right);
                    }else if(-cY / cX < 2.144)
                    {
                        RockerDown(RockerDirection.topRight);
                    }
                    else if(-cY / cX >= 2.144)
                    {
                        RockerDown(RockerDirection.top);
                    }
                }else if (cX < 0 && cY >= 0)
                {
                    // 左下
                    if (-cY / cX <= 0.466)
                    {
                        RockerDown(RockerDirection.left);
                    }
                    else if (-cY / cX < 2.144)
                    {
                        RockerDown(RockerDirection.bottomLeft);
                    }
                    else if (-cY / cX >= 2.144)
                    {
                        RockerDown(RockerDirection.bottom);
                    }
                }
                else if(cX > 0 && cY > 0)
                {
                    // 右下
                    if (cY / cX <= 0.466)
                    {
                        RockerDown(RockerDirection.right);
                    }
                    else if (cY / cX < 2.144)
                    {
                        RockerDown(RockerDirection.bottomRight);
                    }
                    else if (cY / cX >= 2.144)
                    {
                        RockerDown(RockerDirection.bottom);
                    }
                }
                else if(cX < 0 && cY < 0)
                {
                    // 左上
                    if (cY / cX <= 0.466)
                    {
                        RockerDown(RockerDirection.left);
                    }
                    else if (cY / cX < 2.144)
                    {
                        RockerDown(RockerDirection.topLeft);
                    }
                    else if (cY / cX >= 2.144)
                    {
                        RockerDown(RockerDirection.top);
                    }
                }else if (cX == 0)
                {
                    if (cY >= 0)
                    {
                        RockerDown(RockerDirection.bottom);
                    }
                    else
                    {
                        RockerDown(RockerDirection.top);
                    }
                }
            }
        }
        private void RockerDown(RockerDirection direction)
        {
            switch (direction)
            {
                case RockerDirection.top:
                    EventHelper.key_down(Keys.W);
                    EventHelper.key_up(Keys.A);
                    EventHelper.key_up(Keys.S);
                    EventHelper.key_up(Keys.D);
                    break;
                case RockerDirection.topRight:
                    EventHelper.key_down(Keys.W);
                    EventHelper.key_up(Keys.A);
                    EventHelper.key_up(Keys.S);
                    EventHelper.key_down(Keys.D);
                    break;
                case RockerDirection.topLeft:
                    EventHelper.key_down(Keys.W);
                    EventHelper.key_down(Keys.A);
                    EventHelper.key_up(Keys.S);
                    EventHelper.key_up(Keys.D);
                    break;
                case RockerDirection.left:
                    EventHelper.key_up(Keys.W);
                    EventHelper.key_down(Keys.A);
                    EventHelper.key_up(Keys.S);
                    EventHelper.key_up(Keys.D);
                    break;
                case RockerDirection.right:
                    EventHelper.key_up(Keys.W);
                    EventHelper.key_up(Keys.A);
                    EventHelper.key_up(Keys.S);
                    EventHelper.key_down(Keys.D);
                    break;
                case RockerDirection.bottom:
                    EventHelper.key_up(Keys.W);
                    EventHelper.key_up(Keys.A);
                    EventHelper.key_down(Keys.S);
                    EventHelper.key_up(Keys.D);
                    break;
                case RockerDirection.bottomRight:
                    EventHelper.key_up(Keys.W);
                    EventHelper.key_up(Keys.A);
                    EventHelper.key_down(Keys.S);
                    EventHelper.key_down(Keys.D);
                    break;
                case RockerDirection.bottomLeft:
                    EventHelper.key_up(Keys.W);
                    EventHelper.key_down(Keys.A);
                    EventHelper.key_down(Keys.S);
                    EventHelper.key_up(Keys.D);
                    break;
                default:
                    break;
            }
        }
        /**
         * 
         * 
         * 
         * */

        private void Btn_rocker_MouseUp(object sender, MouseEventArgs e)
        {
            rockerRun = false;
            EventHelper.key_up(Keys.W);
            EventHelper.key_up(Keys.A);
            EventHelper.key_up(Keys.S);
            EventHelper.key_up(Keys.D);

        }

        private void Btn_rocker_MouseDown(object sender, MouseEventArgs e)
        {
            rockerStart=new Point(e.X,e.Y);
            rockerRun = true;
        }

        private void SetVisible(bool isVisible)
        {
            this.Visible = isVisible; 
        }
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= (int)WindowStyles.WS_EX_NOACTIVATE | (int)WindowStyles.WS_EX_TOOLWINDOW;
                cp.Parent = IntPtr.Zero; // Keep this line only if you used UserControl
                return cp;
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void 摇杆模式ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
